package com.liujun.io.netty.fourteen.jboss.pri.server;

import java.io.IOException;

import com.liujun.io.netty.fourteen.jboss.pri.NettyConstant;
import com.liujun.io.netty.fourteen.jboss.pri.coder.NettyMessageDecoder;
import com.liujun.io.netty.fourteen.jboss.pri.coder.NettyMessageEncoder;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;

public class NettyServer {

	public void bind() throws Exception {
		EventLoopGroup bossGroup = new NioEventLoopGroup();
		EventLoopGroup workerGroup = new NioEventLoopGroup();

		ServerBootstrap b = new ServerBootstrap();

		b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
				.option(ChannelOption.SO_BACKLOG, 100)
				.handler(new LoggingHandler(LogLevel.INFO))
				.childHandler(new ChannelInitializer<SocketChannel>() {
					@Override
					public void initChannel(SocketChannel ch)
							throws IOException {
						ch.pipeline().addLast(
								new NettyMessageDecoder(1024 * 1024, 4, 4));
						ch.pipeline().addLast(new NettyMessageEncoder());
						ch.pipeline().addLast("readTimeoutHandler",
								new ReadTimeoutHandler(NettyConstant.TIME_OUT));
						ch.pipeline().addLast(new LoginAuthRespHandler());
						ch.pipeline().addLast("HeartBeatHandler",
								new HeartBeatRespHandler());
					}
				});

		b.bind(NettyConstant.REMOTEIP, NettyConstant.PORT).sync();
		System.out.println("Netty server start ok : "
				+ (NettyConstant.REMOTEIP + " : " + NettyConstant.PORT));
	}

	public static void main(String[] args) {
		try {
			new NettyServer().bind();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
